import _ from 'lodash';
// import { encode } from 'html-entities';

// Polyfill for padStart if not available
if (!String.prototype.padStart) {
  String.prototype.padStart = function padStart(targetLength: number, padString: string) {
    targetLength = Math.floor(targetLength) || 0;
    padString = String(padString || ' ');
    if (this.length >= targetLength) {
      return String(this);
    }
    targetLength = targetLength - this.length;
    if (targetLength > padString.length) {
      padString += padString.repeat(targetLength / padString.length);
    }
    return padString.slice(0, targetLength) + String(this);
  };
}

const SPECIAL_CHARS = '\n\r\'"<>&';
const SPECIAL_CHARS_REG = new RegExp(
  `[${SPECIAL_CHARS.split('')
    .map((c) => `\\x${c.charCodeAt(0).toString(16).padStart(2, '0')}`)
    .join('')}]`,
  'g',
);

export function encodeJsxStringNode(str: string): string {
  return str.replace(SPECIAL_CHARS_REG, (c) => `&#${c.charCodeAt(0)};`);
}

// export function encodeJsxStringNode(str: string): string {
//   return encode(str);
// }
